package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.AddressBook;
import com.itheima.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Resource
    private AddressBookService addressBookService;
//接口一： 新增地址
    @PostMapping
    public R<AddressBook> addAddress(@RequestBody AddressBook addressBook){
            addressBook.setUserId(BaseContext.getCurrentId());
        log.info("addressBook:{}", addressBook);
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

//    接口二： 设置默认地址
    @PutMapping("/default")
    public R<AddressBook> updateAddress(@RequestBody AddressBook addressBook) {
        log.info("addressBook:{}", addressBook);
          LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        qw.eq(AddressBook::getUserId, BaseContext.getCurrentId());
          qw.set(AddressBook::getIsDefault,0);
           addressBookService.update(qw);

          addressBook.setIsDefault(1);
        //SQL:update address_book set is_default = 1 where id = ?
        addressBookService.updateById(addressBook);
          return R.success(addressBook);
    }

//接口三： 根据ID查询地址
    @GetMapping("/{id}")
    public R selectById(@PathVariable("id") Long id){

        AddressBook one = addressBookService.getById(id);
        if (one == null) {
            return R.error("no");
        }
        return  R.success(one);
    }
//    接口四： 查询默认地址
    @GetMapping("/default")
    public R<AddressBook> defaultSelect (){
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        qw.eq(AddressBook::getIsDefault,1);
        AddressBook byId = addressBookService.getOne(qw);
        return R.success(byId);
    }

//    接口五： 查询指定用户的全部地址
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("addressBook:{}", addressBook);
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != addressBook.getUserId(), AddressBook::getUserId, addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return R.success(list);
    }

//接口六：删除地址【可能是多个】
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){

        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.in(AddressBook ::getId,ids);

        boolean remove = addressBookService.remove(qw);

        return remove? R.success("ok"):R.error("no");
    }
//    接口七：修改地址
    @PutMapping
    public R update(@RequestBody AddressBook addressBook){

        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook ::getUserId,BaseContext.getCurrentId());
        qw.eq(AddressBook ::getId ,addressBook.getId());
        boolean update = addressBookService.update(addressBook,qw);
        return update ? R.success("ok"):R.error("no");
    }










}
